﻿using System;
using System.Linq;
using Bricks.Caching;
using Spring.Caching;

namespace Bricks.Spring.Caching
{

    /// <summary>
    /// 
    /// </summary>
    public class CacheFacade : CacheFacadeBase<ICache>, ICacheFacade
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cache"></param>
        public CacheFacade(ICache cache) :
            base(() => cache)
        {
            if (cache == null)
            {
                throw new ArgumentNullException("cache");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public override void Add(string key, object value)
        {
            Execute(cache => cache.Insert(key, value));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override bool Contains(string key)
        {
            return Execute<bool>(cache => cache.Keys.Cast<object>().Contains<object>(key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override object Get(string key)
        {
            return Execute<object>(cache => cache.Get(key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public override void Remove(string key)
        {
            Execute(cache => cache.Remove(key));
        }

        /// <summary>
        /// 
        /// </summary>
        public override void Flush()
        {
            Execute(cache => cache.Clear());
        }
    }
}
